package edu.gatech.ors.mrsim.core;

/**
 * Represents an event at a single point in time for a specific module.
 * 
 * @author Andrey K.
 * @param<T> the type of module this originated from
 */
public class Event<N extends Node> implements Comparable<Event<?>> {

	// the timestamp, in ms, when this event occurred
	private long timeStamp;

	private long duration;

	private N sourceNode;

	// Description of the event
	protected String description;

	protected boolean completed;

	public Event(long timeStamp, long duration, N sourceNode, String description) {
		this.timeStamp = timeStamp;
		this.description = description;
		this.duration = duration;
		this.sourceNode = sourceNode;
		completed = false;
	}

	public Event(long timeStamp, N sourceNode, String description) {
		this(timeStamp, 0, sourceNode, description);
	}

	public Event(long timeStamp, N sourceNode) {
		this(timeStamp, 0, sourceNode, "Event created at " + timeStamp + " by " + sourceNode.getName());
	}

	public N getSourceNode() {
		return sourceNode;
	}

	public void setDuration(long setTo) {
		this.duration = setTo;
	}

	public long getDuration() {
		return duration;
	}

	/**
	 * Event e1 is "greater than" event e2 iff e1.getTimeStamp()>e2.getTimeStamp(). This lets PriorityQueue work as expected.
	 */
	public int compareTo(Event<?> e) {
		int dif = (int) (timeStamp - e.timeStamp);
		return dif;
	}

	/**
	 * Getter for timestamp
	 * 
	 * @return timestamp
	 */
	public long getTimeStamp() {
		return timeStamp;
	}

	public void addDuration(long add) {
		this.duration += add;
	}

	@Override
	public String toString() {
		return description + "; duration: " + this.duration;
	}

	public boolean isCompleted() {
		return completed;
	}

	public void setCompleted(boolean setTo) {
		this.completed = setTo;
	}

}